using Common;
using Common.DEncrypt;
using DukeyFileEncoding.Properties;
using Microsoft.VisualStudio.TextTemplating;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Text;
using System.Threading;
using System.Windows.Forms;
namespace DukeyFileEncoding {
    public class MainFrm : Form {
        private Button btnConvert;
        private Button btnSelect;
        private ComboBox cboxbm;
        private CheckBox chkContainChild;
        private IContainer components;
        private string cpuid = "";
        private FolderBrowserDialog folderBrowserDialog1;
        private bool isregister;
        private Thread isRegister;
        private Label label1;
        private Label label2;
        private Label label3;
        private Label label4;
        private Thread thstart;
        private ToolTip toolTip1;
        private TextBox txtFileTypes;
        private TextBox txtFolder;
        private TextBox txtReplace;
        private Label lbltip;
        private WebBrowser webBrowser1;

        public MainFrm() {
            this.InitializeComponent();
            Control.CheckForIllegalCrossThreadCalls = false;
        }

        private void btnConvert_Click(object sender, EventArgs e) {
            this.thstart = new Thread(new ThreadStart(this.Process));
            this.thstart.Start();
        }

        private void btnSelect_Click(object sender, EventArgs e) {
            if (this.folderBrowserDialog1.ShowDialog() == DialogResult.OK) {
                this.txtFolder.Text = this.folderBrowserDialog1.SelectedPath;
            }
        }

        private void CheckIsRegister() {
            //if (Settings.Default.usetimes < 30) {
            //    this.isRegister = null;
            //}
            //else {
            //    string regkey = Settings.Default.regkey;
            //    this.cpuid = WMICardGrab.GetCPUId();
            //    if ((regkey == "") || (Common.DEncrypt.DEncrypt.Decrypt(regkey) != this.cpuid)) {
            //        Register register = new Register(this.cpuid);
            //        if (register.ShowDialog() == DialogResult.OK) {
            //            this.Text = this.Text.Replace("[未注册]", "") + "[已注册]";
            //            this.isregister = true;
            //        }
            //        else {
            //            Environment.Exit(0);
            //        }
            //    }
            //    else {
            //        this.isregister = true;
            //        this.Text = this.Text.Replace("[未注册]", "") + "[已注册]";
            //    }
            //}
        }

        private string ConvertCodingPage(string str, Encoding srcEncoding, Encoding dstEncoding) {
            try {
                if (srcEncoding == dstEncoding) {
                    return str;
                }
                byte[] bytes = srcEncoding.GetBytes(str);
                bytes = Encoding.Convert(srcEncoding, dstEncoding, bytes);
                return dstEncoding.GetString(bytes);
            }
            catch (Exception) {
                return str;
            }
        }

        private void ConvertEncoding(string file, Encoding srcEncoding, Encoding dstEncoding) {
            EncodingHelper.GetEncoding(file);
            this.lbltip.Text = "正在转换 编码:" + srcEncoding.EncodingName + " " + file;
            string str = FileHelper.ReadFile(file, srcEncoding);
            str = this.ConvertCodingPage(str, srcEncoding, dstEncoding);
            str = this.ReplaceStr(str);
            FileHelper.WriteFile(file, str, dstEncoding);
        }

        protected override void Dispose(bool disposing) {
            if (disposing && (this.components != null)) {
                this.components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void InitializeComponent() {
            this.components = new System.ComponentModel.Container();
            this.btnSelect = new System.Windows.Forms.Button();
            this.label3 = new System.Windows.Forms.Label();
            this.txtFolder = new System.Windows.Forms.TextBox();
            this.label1 = new System.Windows.Forms.Label();
            this.txtFileTypes = new System.Windows.Forms.TextBox();
            this.chkContainChild = new System.Windows.Forms.CheckBox();
            this.label2 = new System.Windows.Forms.Label();
            this.cboxbm = new System.Windows.Forms.ComboBox();
            this.btnConvert = new System.Windows.Forms.Button();
            this.label4 = new System.Windows.Forms.Label();
            this.folderBrowserDialog1 = new System.Windows.Forms.FolderBrowserDialog();
            this.toolTip1 = new System.Windows.Forms.ToolTip(this.components);
            this.txtReplace = new System.Windows.Forms.TextBox();
            this.webBrowser1 = new System.Windows.Forms.WebBrowser();
            this.lbltip = new System.Windows.Forms.Label();
            this.SuspendLayout();
            // 
            // btnSelect
            // 
            this.btnSelect.Location = new System.Drawing.Point(508, 10);
            this.btnSelect.Name = "btnSelect";
            this.btnSelect.Size = new System.Drawing.Size(75, 23);
            this.btnSelect.TabIndex = 5;
            this.btnSelect.Text = "选择目录";
            this.btnSelect.UseVisualStyleBackColor = true;
            this.btnSelect.Click += new System.EventHandler(this.btnSelect_Click);
            // 
            // label3
            // 
            this.label3.AutoSize = true;
            this.label3.Location = new System.Drawing.Point(17, 15);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(119, 12);
            this.label3.TabIndex = 4;
            this.label3.Text = "需要转换编码的目录:";
            // 
            // txtFolder
            // 
            this.txtFolder.AllowDrop = true;
            this.txtFolder.Location = new System.Drawing.Point(135, 10);
            this.txtFolder.Name = "txtFolder";
            this.txtFolder.Size = new System.Drawing.Size(359, 21);
            this.txtFolder.TabIndex = 3;
            this.toolTip1.SetToolTip(this.txtFolder, "你可以拖动一个目录放在此文本框上面");
            this.txtFolder.DragEnter += new System.Windows.Forms.DragEventHandler(this.txtFolder_DragEnter);
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(17, 54);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(119, 12);
            this.label1.TabIndex = 6;
            this.label1.Text = "需要转码的文件类型:";
            // 
            // txtFileTypes
            // 
            this.txtFileTypes.Location = new System.Drawing.Point(135, 47);
            this.txtFileTypes.Name = "txtFileTypes";
            this.txtFileTypes.Size = new System.Drawing.Size(359, 21);
            this.txtFileTypes.TabIndex = 3;
            this.txtFileTypes.Text = "txt|html|htm|aspx|php|jsp";
            // 
            // chkContainChild
            // 
            this.chkContainChild.AutoSize = true;
            this.chkContainChild.Checked = true;
            this.chkContainChild.CheckState = System.Windows.Forms.CheckState.Checked;
            this.chkContainChild.Location = new System.Drawing.Point(508, 49);
            this.chkContainChild.Name = "chkContainChild";
            this.chkContainChild.Size = new System.Drawing.Size(84, 16);
            this.chkContainChild.TabIndex = 7;
            this.chkContainChild.Text = "包含子目录";
            this.chkContainChild.UseVisualStyleBackColor = true;
            // 
            // label2
            // 
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(29, 82);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(107, 12);
            this.label2.TabIndex = 6;
            this.label2.Text = "更改后的文件编码:";
            // 
            // cboxbm
            // 
            this.cboxbm.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            this.cboxbm.FormattingEnabled = true;
            this.cboxbm.Items.AddRange(new object[] {
            "ASCII",
            "Unicode",
            "Unicode Big Endian",
            "UTF-8",
            "UTF-8 + BOM",
            "gb2312",
            "Big5",
            "GBK"});
            this.cboxbm.Location = new System.Drawing.Point(136, 76);
            this.cboxbm.Name = "cboxbm";
            this.cboxbm.Size = new System.Drawing.Size(285, 20);
            this.cboxbm.TabIndex = 8;
            // 
            // btnConvert
            // 
            this.btnConvert.Location = new System.Drawing.Point(427, 75);
            this.btnConvert.Name = "btnConvert";
            this.btnConvert.Size = new System.Drawing.Size(75, 23);
            this.btnConvert.TabIndex = 9;
            this.btnConvert.Text = "开始转换";
            this.btnConvert.UseVisualStyleBackColor = true;
            this.btnConvert.Click += new System.EventHandler(this.btnConvert_Click);
            // 
            // label4
            // 
            this.label4.AutoSize = true;
            this.label4.Location = new System.Drawing.Point(19, 136);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(107, 12);
            this.label4.TabIndex = 11;
            this.label4.Text = "需要替换的字符串:";
            // 
            // txtReplace
            // 
            this.txtReplace.Location = new System.Drawing.Point(135, 112);
            this.txtReplace.Multiline = true;
            this.txtReplace.Name = "txtReplace";
            this.txtReplace.Size = new System.Drawing.Size(448, 87);
            this.txtReplace.TabIndex = 10;
            this.txtReplace.Text = "gb2312|utf-8";
            this.toolTip1.SetToolTip(this.txtReplace, "文本替换格式为:需要替换的字符+\"|\"+替换后的字符,如有多个用逗号分开.\r\n示例:\"gb2312|utf-8,zh-cn|en\"");
            // 
            // webBrowser1
            // 
            this.webBrowser1.Location = new System.Drawing.Point(31, 199);
            this.webBrowser1.MinimumSize = new System.Drawing.Size(20, 20);
            this.webBrowser1.Name = "webBrowser1";
            this.webBrowser1.Size = new System.Drawing.Size(25, 20);
            this.webBrowser1.TabIndex = 13;
            this.webBrowser1.Url = new System.Uri("http://www.mou18.com/soft/tj.htm?v=filecodng10", System.UriKind.Absolute);
            this.webBrowser1.Visible = false;
            // 
            // lbltip
            // 
            this.lbltip.AutoSize = true;
            this.lbltip.Location = new System.Drawing.Point(136, 214);
            this.lbltip.Name = "lbltip";
            this.lbltip.Size = new System.Drawing.Size(0, 12);
            this.lbltip.TabIndex = 14;
            // 
            // MainFrm
            // 
            this.ClientSize = new System.Drawing.Size(624, 240);
            this.Controls.Add(this.lbltip);
            this.Controls.Add(this.webBrowser1);
            this.Controls.Add(this.label4);
            this.Controls.Add(this.txtReplace);
            this.Controls.Add(this.btnConvert);
            this.Controls.Add(this.cboxbm);
            this.Controls.Add(this.chkContainChild);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.btnSelect);
            this.Controls.Add(this.label3);
            this.Controls.Add(this.txtFileTypes);
            this.Controls.Add(this.txtFolder);
            this.Name = "MainFrm";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "都客编码转换高手 1.0";
            this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.MainFrm_FormClosing);
            this.Load += new System.EventHandler(this.MainFrm_Load);
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) {
            System.Diagnostics.Process.Start("explorer.exe", "http://hi.baidu.com/38809972/blog/item/8948e46294de6cc2e6113af1.html");
        }

        private void MainFrm_FormClosing(object sender, FormClosingEventArgs e) {
            Settings settings1 = Settings.Default;
            settings1.usetimes++;
            Settings.Default.Save();
            Environment.Exit(0);
        }

        private void MainFrm_Load(object sender, EventArgs e) {
            Dictionary<string, string> dictionary = new Dictionary<string, string>();
            dictionary.Add("ASCII", "20127");
            dictionary.Add("简体中文(GB2312) - 代码页 936", "936");
            dictionary.Add("简体中文(GBK)", "54936");
            dictionary.Add("Unicode(UTF-8 带签名) - 代码页 65001", "65001");
            dictionary.Add("Unicode - 代码页 1200", "1200");
            dictionary.Add("Unicode(Big-Endian)", "1201");
            dictionary.Add("Unicode(UTF-8 无签名)", "65001no");
            dictionary.Add("繁体中文(Big5)", "950");
            dictionary.Add("Western European(Windows)", "1252");
            DataTable table = new DataTable();
            table.Columns.Add("name");
            table.Columns.Add("value");
            foreach (KeyValuePair<string, string> pair in dictionary) {
                DataRow row = table.NewRow();
                row[0] = pair.Key;
                row[1] = pair.Value;
                table.Rows.Add(row);
            }
            this.cboxbm.DataSource = table;
            this.cboxbm.DisplayMember = "name";
            this.cboxbm.ValueMember = "value";
            this.cboxbm.SelectedIndex = 0;
        }

        private void Process() {
            lock (this) {
                Encoding encoding;
                this.btnConvert.Enabled = false;
                this.lbltip.Text = "开始转换...";
                if (this.cboxbm.SelectedValue.ToString() == "65001no") {
                    encoding = new UTF8Encoding(false);
                }
                else {
                    encoding = Encoding.GetEncoding(int.Parse(this.cboxbm.SelectedValue.ToString()));
                }
                DirectoryInfo info = new DirectoryInfo(this.txtFolder.Text.Trim());
                string text = this.txtFileTypes.Text;
                if (this.chkContainChild.Checked) {
                    foreach (FileSystemInfo info2 in info.GetFileSystemInfos()) {
                        if (("|" + text + "|").Contains("|" + info2.Extension.Replace(".", "") + "|") && !info2.Attributes.ToString().Contains("Hidden")) {
                            Encoding srcEncoding = EncodingHelper.GetEncoding(info2.FullName);
                            if (srcEncoding != encoding) {
                                this.ConvertEncoding(info2.FullName, srcEncoding, encoding);
                            }
                        }
                    }
                }
                else {
                    foreach (FileInfo info3 in info.GetFiles()) {
                        if (("|" + text + "|").Contains("|" + info3.Extension + "|") && !info3.Attributes.ToString().Contains("Hidden")) {
                            Encoding encoding3 = EncodingHelper.GetEncoding(info3.FullName);
                            if (encoding3 != encoding) {
                                this.ConvertEncoding(info3.FullName, encoding3, encoding);
                            }
                        }
                    }
                }
                this.lbltip.Text = "全部完成!";
                this.btnConvert.Enabled = true;
                this.thstart = null;
            }
        }

        private string ReplaceStr(string str) {
            try {
                foreach (string str2 in this.txtReplace.Text.Trim().Split(new char[] { ',' })) {
                    string oldValue = str2.Split(new char[] { '|' })[0];
                    string newValue = str2.Split(new char[] { '|' })[1];
                    str = str.Replace(oldValue, newValue);
                }
                return str;
            }
            catch {
                return str;
            }
        }

        private void txtFolder_DragEnter(object sender, DragEventArgs e) {
            this.txtFolder.Text = ((Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString();
        }
    }
}

